home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / modules / trackers / shopping.js < prev    next >
Text File  |  2009-10-21  |  14KB  |  310 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* shopping.js
  3.  * Responsible for tracking shopping links
  4.  */
  5.  
  6. var EXPORTED_SYMBOLS = ["shopping"];
  7.  
  8. var shopping = {
  9.     name: "Shopping",
  10.     name_plural: "Shopping",
  11.     color: "#146EB4",
  12.     isInternal: true,
  13.     preview: function(doc, node, data){
  14.         if(data.preview){
  15.             var result = this.json.decode(data.preview);
  16.             var hbox, label, spacer, image;
  17.             switch(result.type){
  18.                 case this.AMAZON:
  19.                     hbox = doc.createElement('hbox');
  20.                     spacer = doc.createElement('spacer');
  21.                     spacer.setAttribute('flex', '1');
  22.                     hbox.appendChild(spacer);
  23.                     image = doc.createElement('image');
  24.                     image.setAttribute("src",
  25.                             "http://ecx.images-amazon.com/images/P/"+result.id +".01.SCMZZZZZZZ.jpg");
  26.                     hbox.appendChild(image);
  27.                     spacer = doc.createElement('spacer');
  28.                     spacer.setAttribute('flex', '1');
  29.                     hbox.appendChild(spacer);
  30.                     node.appendChild(hbox);
  31.  
  32.                     if(result.price){
  33.                         hbox = doc.createElement('hbox');
  34.                         spacer = doc.createElement('spacer');
  35.                         spacer.setAttribute('flex', '1');
  36.                         hbox.appendChild(spacer);
  37.                         label = doc.createElement('label');
  38.                         label.setAttribute("value", result.price);
  39.                         label.setAttribute("style", "color: red; font-size: 14px;");
  40.                         hbox.appendChild(label);
  41.                         spacer = doc.createElement('spacer');
  42.                         spacer.setAttribute('flex', '1');
  43.                         hbox.appendChild(spacer);
  44.                         node.appendChild(hbox);
  45.                     }
  46.                 break;
  47.                 case this.NEWEGG:
  48.                 case this.BESTBUY:
  49.                 case this.WOOT:
  50.                 case this.SHIRTWOOT:
  51.                 case this.KIDSWOOT:
  52.                 case this.WINEWOOT:
  53.                 case this.SELLOUTWOOT:
  54.                 case this.BUY:
  55.                     hbox = doc.createElement('hbox');
  56.                     spacer = doc.createElement('spacer');
  57.                     spacer.setAttribute('flex', '1');
  58.                     hbox.appendChild(spacer);
  59.                     image = doc.createElement('image');
  60.                     image.setAttribute("src", result.img);
  61.                     hbox.appendChild(image);
  62.                     spacer = doc.createElement('spacer');
  63.                     spacer.setAttribute('flex', '1');
  64.                     hbox.appendChild(spacer);
  65.                     node.appendChild(hbox);
  66.  
  67.                     if(result.price){
  68.                         hbox = doc.createElement('hbox');
  69.                         spacer = doc.createElement('spacer');
  70.                         spacer.setAttribute('flex', '1');
  71.                         hbox.appendChild(spacer);
  72.                         label = doc.createElement('label');
  73.                         label.setAttribute("value", result.price);
  74.                         label.setAttribute("style", "color: red; font-size: 14px;");
  75.                         hbox.appendChild(label);
  76.                         spacer = doc.createElement('spacer');
  77.                         spacer.setAttribute('flex', '1');
  78.                         hbox.appendChild(spacer);
  79.                         node.appendChild(hbox);
  80.                     }
  81.                 break;
  82.             }
  83.         }
  84.     },
  85.     onLoadTracks: function(url, doc){
  86.         if(!doc){
  87.             return [];
  88.         }
  89.         var result = [];
  90.         var match = this.pattern.exec(url);
  91.         if(match){
  92.             var obj = {}; 
  93.             var title = "";
  94.             var redirect_url = null;
  95.             switch(match[1]){
  96.                 case this.AMAZON:
  97.                     var asina = /\/([A-Z0-9]{10})($|\/)/.exec(url);
  98.  
  99.                     if(asina){
  100.                         obj.id = asina[1];
  101.                         obj.type = match[1];
  102.                         var a = doc.getElementsByClassName('priceLarge');
  103.                         if(a.length){
  104.                             obj.price = a[0].textContent;
  105.                         }
  106.                         var el = doc.getElementById("btAsinTitle");
  107.                         if(el){
  108.                             var pricetxt = obj.price ? " (" + obj.price + ")" : "";
  109.                             var name = el.textContent;
  110.                             var namelength = obj.price ? 32 : 40;
  111.                             if(name.length > namelength){
  112.                                 name = name.substring(0,namelength) + "...";
  113.                             }
  114.                             title = "Amazon: " + name + pricetxt;
  115.                         } else {
  116.                             title = doc.title;
  117.                         }
  118.                         redirect_url = "http://www.amazon.com/gp/product/" + obj.id + "?ie=UTF8&tag=boomtango-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=" + obj.id;
  119.                     } else {
  120.                         return [];
  121.                     }
  122.                     break;
  123.                 case this.NEWEGG:
  124.                     if(url.substring(0, this.NEWEGGURL.length) == this.NEWEGGURL){
  125.                         var a =  /[\?&]Item=([^&#]*)/.exec(url);
  126.                         if(a){
  127.                             obj.id = a[1];
  128.                             obj.type = match[1];
  129.  
  130.                             var a = doc.getElementsByClassName('zmp');
  131.                             if(a.length){
  132.                                 obj.price = this.trim(a[0].textContent);
  133.                             }
  134.                             var el = doc.getElementById("pclaImagePreview");
  135.                             if(el){
  136.                                 obj.img = el.src;
  137.                             }
  138.  
  139.                             a = doc.getElementsByTagName('h1');
  140.                             if(a.length){
  141.                                 var pricetxt = obj.price ? " (" + obj.price + ")" : "";
  142.                                 var name = this.trim(a[0].textContent);
  143.                                 var namelength = obj.price ? 32 : 40;
  144.                                 if(name.length > namelength){
  145.                                     name = name.substring(0,namelength) + "...";
  146.                                 }
  147.                                 title = "NewEgg: " + name + pricetxt;
  148.                             } else {
  149.                                 title = doc.title;
  150.                             }
  151.                             redirect_url = "http://www.kqzyfj.com/click-3669670-10440897?url=" + encodeURIComponent(url) + "&cjsku=" + obj.id;
  152.                         } else {
  153.                             return [];
  154.                         }
  155.                     } else {
  156.                         return [];
  157.                     }
  158.                     break;
  159.                 case this.BUY:
  160.                     if(url.substring(0, this.BUYURL.length) == this.BUYURL){
  161.                             obj.type = match[1];
  162.  
  163.                             var el = doc.getElementById("productPricing");
  164.                             if(el){
  165.                                 if(el.firstChild &&
  166.                                    el.firstChild.firstChild &&
  167.                                    el.firstChild.firstChild.lastChild &&
  168.                                    el.firstChild.firstChild.lastChild.lastChild
  169.                                    ){
  170.                                 el = el.firstChild.firstChild.lastChild;
  171.                                 if(el.childNodes.length < 2){
  172.                                     el = el.previousSibling;
  173.                                 }
  174.                                 obj.price = this.trim(el.lastChild.textContent);
  175.                             }
  176.                             var el = doc.getElementById("PROD_mainImg");
  177.                             if(el){
  178.                                 obj.img = el.src;
  179.                             }
  180.  
  181.                             var pricetxt = obj.price ? " (" + obj.price + ")" : "";
  182.                             var name = doc.title.substring(0, doc.title.length - 10);
  183.                             var namelength = obj.price ? 32 : 40;
  184.                             if(name.length > namelength){
  185.                                 name = name.substring(0,namelength) + "...";
  186.                             }
  187.                             title = "Buy.com: " + name + pricetxt;
  188.                         } else {
  189.                             return [];
  190.                         }
  191.                     } else {
  192.                         return [];
  193.                     }
  194.                     break;
  195.                 case this.WOOT:
  196.                 case this.KIDSWOOT:
  197.                 case this.WINEWOOT:
  198.                 case this.SHIRTWOOT:
  199.                 case this.SELLOUTWOOT:
  200.                     if(url == this.WOOTURL ||
  201.                        url == ("http://" + this.KIDSWOOT + "/") ||
  202.                        url == ("http://" + this.WINEWOOT + "/") ||
  203.                        url == ("http://" + this.SHIRTWOOT + "/") ||
  204.                        url == ("http://" + this.SELLOUTWOOT + "/") ||
  205.                        url.substring(0,this.SELLOUTURL.length) == this.SELLOUTURL){
  206.                         obj.type = match[1];
  207.  
  208.                         var el = doc.getElementsByClassName("productDescription");
  209.                         if(el.length){
  210.                             var el2 = el[0].getElementsByTagName("h2");
  211.                             var name;
  212.                             if(el2.length){
  213.                                 name = this.trim(el2[0].textContent);
  214.  
  215.                                 el2 = el[0].getElementsByClassName("amount");
  216.                                 if(el2.length){
  217.                                     obj.price = "$" + this.trim(el2[0].textContent);
  218.                                 }
  219.                             } else {
  220.                                 name = "Woot.com";
  221.                             }
  222.  
  223.                             var el2 = doc.getElementsByClassName("photo");
  224.                             if(el2.length){
  225.                                 obj.img = el2[0].src;
  226.                             }
  227.  
  228.                             var pricetxt = obj.price ? " (" + obj.price + ")" : "";
  229.                             var namelength = obj.price ? 32 : 40;
  230.                             if(name.length > namelength){
  231.                                 name = name.substring(0,namelength) + "...";
  232.                             }
  233.                             title = "Woot: " + name + pricetxt;
  234.                         } else {
  235.                             return [];
  236.                         }
  237.                     } else {
  238.                         return [];
  239.                     }
  240.                     break;
  241.                 case this.BESTBUY:
  242.                     if(url.substring(0, this.BESTBUYURL.length) == this.BESTBUYURL){
  243.                         obj.type = match[1];
  244.  
  245.                         var el = doc.getElementsByClassName("salenum");
  246.                         if(el.length){
  247.                             obj.price = this.trim(el[0].textContent);
  248.                         }
  249.                         var el = doc.getElementById("imagepreview");
  250.                         if(el){
  251.                             el = el.getElementsByTagName('img');
  252.                             if(el.length){
  253.                                 obj.img = el[0].src;
  254.                             }
  255.                         }
  256.  
  257.                         var pricetxt = obj.price ? " (" + obj.price + ")" : "";
  258.                         var name = doc.title;
  259.                         var namelength = obj.price ? 32 : 40;
  260.                         if(name.length > namelength){
  261.                             name = name.substring(0,namelength) + "...";
  262.                         }
  263.                         title = "BestBuy: " + name + pricetxt;
  264.                     } else {
  265.                         return [];
  266.                     }
  267.                     break;
  268.                 default:
  269.                     return [];
  270.             }
  271.  
  272.             result.push(
  273.                 {
  274.                     type: "shopping",
  275.                     title: title,
  276.                     redirect_url: redirect_url,
  277.                     preview: this.json.encode(obj)
  278.                 }
  279.             );
  280.         }
  281.         return result;
  282.     },
  283.     trim: function (str) {
  284.         if(str.trim){
  285.             return str.trim();
  286.         }
  287.         var str = str.replace(/^\s\s*/, ''),
  288.                 ws = /\s/,
  289.                     i = str.length;
  290.         while (ws.test(str.charAt(--i)));
  291.         return str.slice(0, i + 1);
  292.     },
  293.     json: Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON),
  294.     AMAZON: "www.amazon.com",
  295.     NEWEGG: "www.newegg.com",
  296.     BESTBUY: "www.bestbuy.com",
  297.     BUY: "www.buy.com",
  298.     WOOT: "www.woot.com",
  299.     SHIRTWOOT: "shirt.woot.com",
  300.     WINEWOOT: "wine.woot.com",
  301.     KIDSWOOT: "kids.woot.com",
  302.     SELLOUTWOOT: "sellout.woot.com",
  303.     WOOTURL: "http://www.woot.com/",
  304.     SELLOUTURL: "http://sellout.woot.com/Default.aspx",
  305.     BUYURL: "http://www.buy.com/prod",
  306.     BESTBUYURL: "http://www.bestbuy.com/site/olspage.jsp?skuId=",
  307.     NEWEGGURL: "http://www.newegg.com/Product/Product.aspx?Item=",
  308.     pattern: /^http(?:s*):\/\/(shirt.woot.com|wine.woot.com|kids.woot.com|sellout.woot.com|www.woot.com|www.amazon.com|www.newegg.com|www.buy.com|www.bestbuy.com)/
  309. };
  310.